package com.lz.football.module.activity.matchWelfare
{
	import com.common.utils.Reflection;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.ActivityEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.model.AppData;
	import com.lz.football.module.activity.IActivity;
	
	import common.BaseView.BaseView;
	import common.BaseView.GoodItem;
	
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.text.TextField;
	
	public class MatchWelfareView extends BaseView implements IActivity
	{
		private var awardList:Array = [[5,1],[5,8],[5,15],[7,24],[7,13]];
		private var itemList:Array;
		private var btn_1:SimpleButton;
		private var btn_2:SimpleButton;
		private var btn_3:SimpleButton;
		private var btn_4:SimpleButton;
		private var btn_5:SimpleButton;
		public function MatchWelfareView()
		{
			super();
		}
		
		override public function initUI(view:Object=null):void
		{
			symbolSkin = Reflection.createInstance("com.lz.res.activity.MatchWelfareView");
			this.addChild(symbolSkin);
			_windowBg = symbolSkin.matchWelfareBg;
			_viewHeight = _windowBg.height;
			_viewWidth = _windowBg.width;
			btn_1 = symbolSkin.btn_1
			btn_2 = symbolSkin.btn_2;
			btn_3 = symbolSkin.btn_3;
			btn_4 = symbolSkin.btn_4;
			btn_5 = symbolSkin.btn_5;
			btn_1.mouseEnabled = btn_2.mouseEnabled =btn_3.mouseEnabled =btn_4.mouseEnabled =btn_5.mouseEnabled  = false;
			isCloseBtn = true;
			isDrag = true;
			moveClose(_viewWidth - 35,35);
			onResizeHandler();
			addEvtListener();
			initItem();
			symbolSkin.matchTime.text = "";
			
			var time:TextField = symbolSkin.time;
			var xml:XML = ResourceManager.instance.getResource(ResourceConst.ACTIVITYTIME_XML);
			time.text = "活动时间：" +xml.activity8 + "截止";;
//			baseinformation(null);
		}
		private function initItem():void
		{
			var item:GoodItem;
			itemList = [];
			var len:int = awardList.length;
			for(var i:int = 0;i < len;i++)
			{
				item = new GoodItem();
				item.initUI();
				item.data = AppData.getInstance().goodsManager.getGoodsByTypeAndId(awardList[i][0],awardList[i][1]);
				item.x = 56 + i * 128;
				item.y = 250;
				this.addChild(item);
				itemList.push(item);
			}
		}
		override public function addEvtListener():void
		{
			btn_1.addEventListener(MouseEvent.CLICK,onGetHandler);
			btn_2.addEventListener(MouseEvent.CLICK,onGetHandler);
			btn_3.addEventListener(MouseEvent.CLICK,onGetHandler);
			btn_4.addEventListener(MouseEvent.CLICK,onGetHandler);
			btn_5.addEventListener(MouseEvent.CLICK,onGetHandler);
		}
		/**
		 *领取奖励 
		 * @param event
		 * 
		 */
		protected function onGetHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			var type:int;
			switch(event.currentTarget)
			{
				case btn_1:
					type = 1;
					break;
				case btn_2:
					type = 2;
					break;
				case btn_3:
					type = 3;
					break;
				case btn_4:
					type = 4;
					break;
				case btn_5:
					type = 5;
					break;
			}
			this.dispatchEvent(new ActivityEvent(ActivityEvent.ACTIVITY_MATCHWELFARE_AWARD,{type:type}));
		}
		override public function removeEvtListener():void
		{
			btn_1.removeEventListener(MouseEvent.CLICK,onGetHandler);
			btn_2.removeEventListener(MouseEvent.CLICK,onGetHandler);
			btn_3.removeEventListener(MouseEvent.CLICK,onGetHandler);
			btn_4.removeEventListener(MouseEvent.CLICK,onGetHandler);
			btn_5.removeEventListener(MouseEvent.CLICK,onGetHandler);
			super.removeEvtListener();
		}
		
		override public function destroyView():void
		{
			removeEvtListener();
			var item:GoodItem;
			while(itemList.length)
			{
				item = itemList.pop();
				item.destroyView();
				item = null;
			}
			itemList = null;	
			super.destroyView();
		}
		override public function onCloseHandler(event:MouseEvent):void
		{
			this.dispatchEvent(new ActivityEvent(ModuleEvent.REMOVE_MODULE));
		}
		/**
		 *信息 
		 * @param $obj
		 * 
		 */
		public function baseinformation($obj:Object):void
		{
//			btn_1.filters = btn_2.filters =btn_3.filters =btn_4.filters =btn_5.filters = [new ColorMatrixFilter([0.3086, 0.6094, 0.082, 0, 0,
//				0.3086, 0.6094, 0.082, 0, 0,
//				0.3086, 0.6094, 0.082, 0, 0,
//				0,0,0,1,0,])];
//			btn_1.mouseEnabled = btn_2.mouseEnabled =btn_3.mouseEnabled =btn_4.mouseEnabled =btn_5.mouseEnabled  = false;
//			
//			btn_1.visible = false;
			
			symbolSkin.matchTime.text = $obj.matchcount;
			setButtonStatus(btn_1,$obj.status8);
			setButtonStatus(btn_2,$obj.status9);
			setButtonStatus(btn_3,$obj.status10);
			setButtonStatus(btn_4,$obj.status11);
			setButtonStatus(btn_5,$obj.status12);
		}
		/**
		 *设置按钮状态 
		 * 
		 */
		private function setButtonStatus($btn:SimpleButton,value:int):void
		{
			switch(value)
			{
				case 0:
					$btn.filters = [new ColorMatrixFilter([0.3086, 0.6094, 0.082, 0, 0,
						0.3086, 0.6094, 0.082, 0, 0,
						0.3086, 0.6094, 0.082, 0, 0,
						0,0,0,1,0,])];
					$btn.mouseEnabled = false;
					
					break;
				case 1:
					$btn.filters = null;
					$btn.mouseEnabled = true;
					
					break;
				case 2:
					$btn.visible = false;
					$btn.mouseEnabled = false;
					$btn.filters = null;
					break;
			}
		}
		/**
		 *领取奖励返回  
		 * @param $obj
		 * 
		 */		
		public function award($obj:Object):void
		{
			switch($obj.type)
			{
				case 1:
					setButtonStatus(btn_1,2);
					break;
				case 2:
					setButtonStatus(btn_2,2);
					break;
				case 3:
					setButtonStatus(btn_3,2);
					break;
				case 4:
					setButtonStatus(btn_4,2);
					break;
				case 5:
					setButtonStatus(btn_5,2);
					break;
			}
		}
	}
}